<?php

namespace App\Http\Controllers\Company;

use App\Http\Controllers\Core\HomeController as Home;
use App\Models\CompanyProfile;
use App\Models\Job;
use App\Models\JobResumeAccess;
use Illuminate\Http\Request;

class ResumeController extends Home
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //$this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @param int $status
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index($status = 1,Request $request)
    {
       // $status = $request->get('status')?$request->get('status'):1;
        if (!$this->isLogin()) {
            return redirect('login');
        }
        if ($this->member->type != 1) {
            return redirect('login');
        }
        $user_id = isSet($this->member->id) ? $this->member->id : 0;
        //获取公司信息
        $company = CompanyProfile::select('id')->where(['user_id' => $user_id])->get();
        if (!$company = $company->first()) {
            return redirect('login');
        }
        //获取筛选条件
        $params = $request->all();
        //根据公司ID 获取用户信息列表
        $list = JobResumeAccess::join('resume', 'job_resume_access.resume_id', '=', 'resume.id', 'left')
            ->where('job_resume_access.company_id', $company->id)
            ->join('jobs', 'job_resume_access.job_id', '=', 'jobs.id', 'left')
            ->select("job_resume_access.id","jobs.id as job_id", "job_resume_access.status", "resume.nickname", 'resume.create_time', "resume.birth", "resume.sex", "resume.education_cn", "resume.education_cn", 'jobs.jobs_name');
        if (isSet($params['key'])) {
            $list = $list->where('jobs.jobs_name', 'like', '%' . $params['key'] . '%');
        }
        if ($status == 1) {
            $list = $list->where(['job_resume_access.status' => 1]);
        } else {
            $list = $list->where('job_resume_access.status', '!=', 1);
        }
        $list = $list->paginate(10);
        $data = [
            'status' => $status,
            'list' => $list,
            'params' => $params
        ];
        return view('company.resume.index', $data);
    }

    /**
     *  新增岗位
     */
    public function add()
    {
        if (!$this->isLogin()) {
            return redirect('login');
        }
        if ($this->member->type != 1) {
            return redirect('login');
        }
        return view('company.resume.add');
    }

    public function interview(Request $request)
    {
        if (!$this->isLogin()) {
            return redirect('login');
        }
        if ($this->member->type != 1) {
            return redirect('login');
        }
        $user_id = isSet($this->member->id) ? $this->member->id : 0;
        //获取公司信息
        $company = CompanyProfile::select('id')->where(['user_id' => $user_id])->get();
        if (!$company = $company->first()) {
            return false;
        }
        //TODO 获取筛选条件
        $params = $request->all();
        $params['status'] = 3;
        //根据公司ID 获取用户信息列表
        $list = JobResumeAccess::getInformation($params);

        //获取用户全部职位
        //用户状态
        $data = [
            'list' => $list,
            'params' => $params
        ];
        return view('company.resume.interview', $data);
    }

    public function favorites()
    {
        if (!$this->isLogin()) {
            return redirect('login');
        }
        if ($this->member->type != 1) {
            return redirect('login');
        }
        return view('company.resume.favorites');
    }

    public function log()
    {
        if (!$this->isLogin()) {
            return redirect('login');
        }
        if ($this->member->type != 1) {
            return redirect('login');
        }
        return view('company.resume.log');
    }

    /*
     * 面试邀请 或者拒绝
     * */
    public function status(Request $request)
    {
        if (!$this->isLogin()) {
            return redirect('login');
        }
        if ($this->member->type != 1) {
            return redirect('login');
        }
        $id = $request->get('id');
        $status = $request->get('status');
        //变更处理状态 ，并且变更统计数据
        $access = JobResumeAccess::where(['status' => 1])->find($id);
        if (!$access) {
            return json_encode(['data' => '', 'msg' => '状态错误，请刷新后尝试', 'status' => 0]);
        }
        if (!in_array($status, [3, 4])) {
            return json_encode(['data' => '', 'msg' => '参数错误', 'status' => 0]);
        }
        $access->status = $status;
        if (!$access->save()) {
            return json_encode(['data' => '', 'msg' => '数据更新错误', 'status' => 0]);
        }
        //变更工作
        $job = Job::find($access->job_id);
        $job->dispose_number--;

        $job->press = !$job->resume_number ? "100" : number_format(($job->resume_number - $job->dispose_number) / $job->resume_number, 2);
        if (!$job->save()) {
            return json_encode(['data' => '', 'msg' => '数据更新错误', 'status' => 0]);
        }
        return json_encode(['data' => '', 'msg' => '成功', 'status' => 1]);
    }

    /**
     * 根据ID逻辑删除关系数据
     * @param $id
     * @return false|string
     */
    public function interview_delete($id = 0)
    {
        $access = JobResumeAccess::find($id);
        if (!$access) {
            return $this->retJson(0, '参数错误');
        }
        $access->company_status = 0;
        if ($access->save()) {
            return $this->retJson(1, '成功');
        }
        return $this->retJson(0, '数据变更失败');
    }

    /**
     * refresh 刷新简历
     */
    public function refresh()
    {
        if (!$this->isLogin()) {
            return redirect('login');
        }
        if ($this->member->type != 1) {
            return redirect('login');
        }
        $user_id = $this->getUserId();
        //通过user_id获取company_id
        $company = CompanyProfile::where(['user_id' => $user_id])->get()->first();
        $company_id = isSet($company->id) ? $company->id : 0;
        Job::where('company_id', $company_id)->where(['display' => 1])->update(['refresh_time' => time()]);
    }
}